/*  
     PlAr is Platform Arena: a 2D multiplayer shooting game
     Copyright (C) 2009 Ragagnin Antonio <spocchio@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package plar.core;



import org.jbox2d.common.Vec2;

import plar.core.Common;
import plar.core.Element;
import plar.core.Parameters;

public class Gun extends Element {

	int ammo;
	public Player owner;
	String gunName;
	public Gun(Player player) {
		super();
		owner = player;
		ammo = 0;
		gunName = new String("Gun");
	     setProperty("gunName",gunName);
		setProperty("shootSpeed", 9);
	}

public String getGunName()
{
		return (String)getProperty("gunName");
}
public int getAmmo()
{
		return ammo;
}
public void initializeShoot(Shoot shoot) {

	
	shoot.setProperty("owner", this);
	Vec2 shootPos = getShootRelativePosition();
	Vec2 shootSpeed = new Vec2();
	
	Integer s = (Integer) (getProperty("shootSpeed"));
	shootSpeed.x=D().x*s;
	shootSpeed.y=D().y*s;
	Common.info(10," relative position of the shoot:"+shootPos+" cmd:"+owner.body.getWorldCenter()+" absolute shoot position:"+owner.body.getWorldVector(shootPos));
	Common.info(10," relative speed of the shoot:"+shootSpeed);
	shootPos.x+= owner.body.getWorldCenter().x;
	shootPos.y+= owner.body.getWorldCenter().y;
	
	shoot.setPosition(shootPos.x,shootPos.y);
	owner.level.AddElement(shoot);
	shoot.applySpeed(shootSpeed);
	
	
}
	public void addAmmo(int i) {
		ammo += i;

	}

	public void fire() {
	
	}

	public Vec2 D()
	{
		if(owner.getProperty("directionx")!=null &&  owner.getProperty("directiony")!=null)
		{
		Float dx =  ((Double)owner.getProperty("directionx")).floatValue();
		Float dy =    ((Double)owner.getProperty("directiony")).floatValue();
		
		Vec2 d=  new Vec2(dx,dy);
		Common.info(10," player direction: "+d);
		return d;
		}else
		return new Vec2(1,0);
	}
	
	public Vec2 getShootRelativePosition() {
		Vec2 pos = new Vec2(0,0);
		//Double d = (Double) owner.getProperty("shootDirection");
		
		//if(d<=0.5) d= 0.0; else d=1.0;
		pos.x = D().x * (owner.getSize().x*2 );
		pos.y = D().y * (owner.getSize().y*2 );
		
		return pos;
	}


}
